<?php

namespace App\Http\Controllers\SystemManagement;

use App\Http\Controllers\IndexController;
use App\Models\Constant\Fields;
use App\Models\SystemManagement\Manager;
use Illuminate\Http\Request;

/**
 * Class ManagerController
 * @package App\Http\Controllers\SystemManagement
 * @group 用户管理
 */
class ManagerController extends IndexController
{
    /**
     * 新增用户
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     *
     * @bodyParam id int required ID
     * @bodyParam account 用户账号
     * @bodyParam name 用户姓名
     * @bodyParam type 用户类型
     * @bodyParam contact 联系对象
     * @bodyParam department_id 所属部门ID 外键 部门表
     * @bodyParam phone 联系电话
     * @bodyParam description 用户说明
     *
     * @response 201 {"code":201,"msg":"created","data":"新增成功"}
     * @response 400 {"code":400,"msg":"param error","data":"请添加数据/新增失败"}
     */
    public function createManager(\App\Http\Requests\System\Manager $request){
        return $this->create($request,new Manager());
    }

    /**
     * 用户列表-带分页
     * @return false|string
     *
     * @queryParam id 用户ID
     * @queryParam account 用户账号
     * @queryParam name 用户姓名
     * @queryParam type 用户类型
     * @queryParam contact 联系对象
     * @queryParam department_id 所属部门ID 外键 部门表
     * @queryParam phone 联系电话
     * @queryParam description 用户说明
     *
     * @response 200 {code":200,"msg":"success","data":"成功"}
     * @response 404 {"code":404,"msg":"not found","data":"没有查询到任何数据"}
     */
    public function getManager(){
        $per = \request()->input('per_page');
        return $this->getList(new Manager(),$per);
    }

    /**
     * 用户列表-不带分页
     * @return \Illuminate\Http\JsonResponse
     *
     * @queryParam id 用户ID
     * @queryParam account 用户账号
     * @queryParam name 用户姓名
     * @queryParam type 用户类型
     * @queryParam contact 联系对象
     * @queryParam department_id 所属部门ID 外键 部门表
     * @queryParam phone 联系电话
     * @queryParam description 用户说明
     *
     * @response 200 {code":200,"msg":"success","data":"成功"}
     * @response 404 {"code":404,"msg":"not found","data":"没有查询到任何数据"}
     */
    public function getAll(){
        return response()->json(
            encodeMsg(Manager::get(),200)
        );
    }

    /**
     * 查看用户
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     *
     * @queryParam id 用户ID
     * @queryParam account 用户账号
     * @queryParam name 用户姓名
     * @queryParam type 用户类型
     * @queryParam contact 联系对象
     * @queryParam department_id 所属部门ID 外键 部门表
     * @queryParam phone 联系电话
     * @queryParam description 用户说明
     *
     * @response 200 {code":200,"msg":"success","data":"成功"}
     * @response 404 {"code":404,"msg":"not found","data":"该用户不存在"}
     */
    public function getOneManager($id){
        return $this->show($id,new Manager());
    }

    /**
     * 修改用户
     * @param Request $request
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     *
     * @bodyParam id int required ID
     * @bodyParam account 用户账号
     * @bodyParam name 用户姓名
     * @bodyParam type 用户类型
     * @bodyParam contact 联系对象
     * @bodyParam department_id 所属部门ID 外键 部门表
     * @bodyParam phone 联系电话
     * @bodyParam description 用户说明
     *
     * @response 205 {code":205,"msg":"updated","data":"成功"}
     * @response 400 {"code":400,"msg":"parameter error","data":"失败"}
     * @response 404 {"code":404,"msg":"not found","data":"该用户不存在"}
     */
    public function updateManager(\App\Http\Requests\System\Manager $request,$id){
        return $this->update($request,$id,new Manager());
    }

    /**
     * 删除用户
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     *
     * @bodyParam ids array required id数组
     *
     * @response 204 {code":204,"msg":"deleted","data":"成功"}
     * @response 400 {"code":400,"msg":"parameter error","data":"失败"}
     */
    public function del(Request $request){
        $ids = $request->input('ids');
        foreach ($ids as $k=>$id){
            $ids[$k] = (int) $id;
        }
        try{
            Manager::whereIn(Fields::ID,$ids)->delete();
            return response()->json(encodeMsg("删除成功",204));
        }catch (\Exception $e){
            //dd($e->getMessage());
            return response()->json(encodeMsg("删除失败",400));
        }
    }

    /**
     * 恢复数据
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     *
     * @bodyParam ids array required id数组
     *
     * @response 205 {code":205,"msg":"updated","data":"成功"}
     * @response 400 {"code":400,"msg":"parameter error","data":"失败"}
     */
    public function rec(Request $request){
        return $this->recover($request,new Manager());
    }

    /**
     * 按名称搜索
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     *
     * @bodyParam name string required 名称
     *
     * @queryParam id 用户ID
     * @queryParam account 用户账号
     * @queryParam name 用户姓名
     * @queryParam type 用户类型
     * @queryParam contact 联系对象
     * @queryParam department_id 所属部门ID 外键 部门表
     * @queryParam phone 联系电话
     * @queryParam description 用户说明
     *
     * @response 200 {code":200,"msg":"success","data":"成功"}
     * @response 400 {"code":400,"msg":"parameter error","data":"失败"}
     * @response 404 {"code":404,"msg":"not found","data":"没有查询到任何数据"}
     */
    public function getBy(Request $request){
        return $this->getByCond($request,new Manager());
    }
}
